package com.kye.express.business.welfare;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.App;
import com.kye.express.R;
import com.kye.express.business.jd.shop.JDShopActivity;
import com.kye.express.business.tcl.home.TCLActivity;
import com.kye.express.business.vipshop.VipExchangeActivity;
import com.kye.express.business.wechat_redpacket.WeChatPacketExchangeActivity;
import com.kye.express.databinding.ItemWelfareBinding;
import com.kye.express.utils.NetUtils;
import com.kye.express.view.ToastManager;
import com.kye.model.SharedPreferencesConfig;
import com.kye.model.SharedPreferencesManager;
import com.kye.model.bean.welfare.ResponseWelfareLiPinDH;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * 礼品界面-商城模块Adapter
 * <p>
 * Created by SkyYu on 2017/12/19
 */
public class WelfareItemViewBinder extends ItemViewBinder<ResponseWelfareLiPinDH.ResultBean, WelfareItemViewBinder.ViewHolder> {

    private Context mContext;

    WelfareItemViewBinder(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    protected WelfareItemViewBinder.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_welfare, parent, false);
        return new WelfareItemViewBinder.ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ResponseWelfareLiPinDH.ResultBean item) {
        holder.bindData(item);
        Glide.with(App.getContext())
                .load(item.getModelIcon())
                .apply(new RequestOptions().error(R.drawable.error).placeholder(R.drawable.background_about_empty))
                .into(holder.imageView);
        holder.textView.setTextColor(Color.parseColor("#" + item.getModelTitleColor()));
        holder.textViewLine.setBackgroundColor(Color.parseColor("#" + item.getModelTitleColor()));
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ResponseWelfareLiPinDH.ResultBean data;
        private ItemWelfareBinding dataBinding;
        private ImageView imageView;
        private TextView textView;
        private TextView textViewLine;

        private ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
            imageView = itemView.findViewById(R.id.imageview_welfare_item_pic);
            textView = itemView.findViewById(R.id.textview_welfare_item_title);
            textViewLine = itemView.findViewById(R.id.textview_welfare_item_line);
        }

        private void bindData(ResponseWelfareLiPinDH.ResultBean item) {
            data = item;
            dataBinding.setPlatformData(data);

            RxView.clicks(dataBinding.getRoot())
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (NetUtils.isNetworkAvailable(mContext)) {
                                if (data.getModelTitle().contains("京东")) {
                                    JDShopActivity.startActivity(mContext);
                                    return;
                                }
                                if (data.getModelTitle().contains("TCL")) {
                                    boolean isCanInterShop = SharedPreferencesManager.get(SharedPreferencesConfig.KEY_IS_CAN_INTER_SHOP,true);
                                    if(isCanInterShop){
                                        TCLActivity.startActivity(mContext);
                                    }else{
                                        ToastManager.show(mContext,"升级中，敬请期待");
                                    }
                                    return;
                                }
                                if (data.getModelTitle().contains("微信")) {
//                                    ToastManager.show(mContext, "微信红包");
                                    WeChatPacketExchangeActivity.startActivity(mContext);
                                    return;
                                }
                                if (data.getModelTitle().contains("唯品会")) {
//                                    ToastManager.show(mContext, "唯品会");
                                    VipExchangeActivity.startActivity(mContext);
                                    return;
                                }
                                if (data.getModelTitle().contains("跨越速购商城")) {
                                    ToastManager.show(mContext, "跨越速购商城");
                                    return;
                                }
                                if (data.getModelTitle().contains("格力商城")) {
                                    ToastManager.show(mContext, "格力商城");
                                }
                            } else {
                                ToastManager.show(mContext, mContext.getString(R.string.common_net_error));
                            }
                        }
                    });
        }
    }

}